package chz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/bus-routes")
public class BusRouteController {
    @Autowired
    private BusRouteService busRouteService;

    @GetMapping
    public List<BusRoute> getAllBusRoutes() {
        return busRouteService.list();
    }

    @GetMapping("/{id}")
    public BusRoute getBusRouteById(@PathVariable Long id) {
        return busRouteService.getById(id);
    }

    @GetMapping("/by-name")
    public List<BusRoute> getBusRoutesByName(@RequestParam String name) {
        QueryWrapper<BusRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("route_name", name);
        return busRouteService.list(queryWrapper);
    }

    @GetMapping("/by-number")
    public List<BusRoute> getBusRoutesByNumber(@RequestParam String number) {
        QueryWrapper<BusRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("route_number", number);
        return busRouteService.list(queryWrapper);
    }

    @PostMapping
    public String addBusRoute(@RequestBody BusRoute busRoute) {
        boolean saved = busRouteService.save(busRoute);
        return saved ? "Bus route added successfully" : "Failed to add bus route";
    }

    @PutMapping("/{id}")
    public String updateBusRoute(@PathVariable Long id, @RequestBody BusRoute busRoute) {
        busRoute.setId(id);
        boolean updated = busRouteService.updateById(busRoute);
        return updated ? "Bus route updated successfully" : "Failed to update bus route";
    }

    @DeleteMapping("/{id}")
    public String deleteBusRoute(@PathVariable Long id) {
        boolean removed = busRouteService.removeById(id);
        return removed ? "Bus route deleted successfully" : "Failed to delete bus route";
    }
}